package com.qiezi.project.fragment;



import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.qiezi.R;
import com.qiezi.base.BaseMvpFragment;
import com.qiezi.base.BaseRecyclerAdapter;
import com.qiezi.bean.CategoryTwoBean;
import com.qiezi.mvp.CreatePresenter;
import com.qiezi.project.activity.RecyListActivity;
import com.qiezi.project.adapter.CategoryAdapter;
import com.qiezi.project.contract.CategoryContract;
import com.qiezi.project.decoration.RecyclerItemDecoration;

import java.util.ArrayList;

import butterknife.BindView;

@CreatePresenter(presenter = CategoryContract.Presenter.class)
public class CategoryOneFragment extends BaseMvpFragment<CategoryContract.Presenter> implements CategoryContract.View,SwipeRefreshLayout.OnRefreshListener {



    @BindView(R.id.rcv)
    RecyclerView rcv;

    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;


    CategoryAdapter adapter;

    private int id;

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public void scrollToTop() {
        if (rcv != null)
            rcv.scrollToPosition(0);
    }

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_single_recy;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        rcv.setLayoutManager(new GridLayoutManager(getActivity(),3));
        adapter = new CategoryAdapter();
        rcv.setAdapter(adapter);
        adapter.setEmptyViewDef(getActivity(),R.drawable.ic_network_error);
        rcv.setItemAnimator(new DefaultItemAnimator());
        rcv.addItemDecoration(new RecyclerItemDecoration(10,3));

        refresh.setColorSchemeResources(R.color.colorRed);
        refresh.setOnRefreshListener(this);
        refresh.setRefreshing(true);
        onRefresh();
    }




    @Override
    protected void initListener() {
        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<CategoryTwoBean>() {
            @Override
            public void onItemClick(int position, CategoryTwoBean data) {
                log(data.toString());
                Intent intent = new Intent(getActivity(),RecyListActivity.class);
                intent.putExtra("data",data);
                intent.putExtra("type",0);
                startActivity(intent);
            }
        });

    }


    @Override
    public void onCanceled() {
        super.onCanceled();
        getPresenter().canceledCell();
    }

    @Override
    public void complete() {
        super.complete();
        if (refresh != null && refresh.isRefreshing()){
            refresh.setRefreshing(false);
        }
        if (adapter.getEmptyVisibility() == View.INVISIBLE){
            adapter.setEmptyVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showError(String msg) {
        super.showError(msg);
        adapter.setEmptyImage(R.drawable.ic_network_error);
        complete();
    }


    @Override
    public void onRefresh(ArrayList<CategoryTwoBean> data) {
        if (data.size() == 0){
            adapter.setEmptyImage(R.drawable.ic_data_empty);
        }
        adapter.setDatas(data);
        complete();
    }



    @Override
    public void onRefresh() {
        getPresenter().loadData(id);
    }
}
